var game={
	score:0,
	RN:4,
	CN:4,
	data:null,
	start:function(){
		this.score=0;
		this.data=[];
		for(var i=0;i<this.RN;i++){
			this.data.push([]);
			for(var j=0;j<this.CN;j++){
				this.data[i][j]=0;
			}
		}
		//console.log(this.data.join(";"));
		this.randomNum();
		this.randomNum();
		//console.log(this.data.join(";"));
		this.updataView();
		var self=this;
		document.onkeydown=function(){
			var e=window.event||arguments[0];
			var code=e.keyCode;
			switch(code){
				case 37:self.moveLeft();break;
				case 38:self.moveUp();break;
				case 39:self.moveRight();break;
				case 40:self.moveDown();break;
			}
		}
	},
	randomNum:function(){
		var num=Math.random()>0.5?4:2;
		while(true){
			if(this.isFull()){
				return;
			}
			var i=parseInt(Math.random()*this.CN);
			var j=parseInt(Math.random()*this.RN);
			if(this.data[i][j]==0){
				this.data[i][j]=num;
				break;
			}
		}
	},
	updataView:function(){
		for(var i=0;i<this.RN;i++){
			for(var j=0;j<this.CN;j++){
				var str="c"+i+j;
				var cell=document.getElementById(str);
				if(this.data[i][j]>0){
					cell.innerHTML=this.data[i][j];
					cell.className="cell n"+this.data[i][j];
				}else{
					cell.innerHTML="";
					cell.className="cell";
				}
			}
		}
		var score=document.getElementById("score");
		score.innerHTML=this.score;
		//parseInt(score.innerHTML);
	},
	isFull:function(){
		for(var i=0;i<this.RN;i++){
			for(var j=0;j<this.CN;j++){
				if(this.data[i][j]==0){
					return false;
				}
			}
		}
		return true;
	},
	moveUp:function(){
		for(var c=0;c<this.CN;c++){
			this.UpInCol(c);
		}
		this.randomNum();
		this.updataView();
	},
	moveDown:function(){
		for(var c=0;c<this.CN;c++){
			this.DownInCol(c);
		}
		this.randomNum();
		this.updataView();
	},
	moveLeft:function(){
		for(var r=0;r<this.RN;r++){
			this.LeftInRow(r);
		}
		this.randomNum();
		this.updataView();
	},
	moveRight:function(){
		for(var r=0;r<this.RN;r++){
			this.RightInRow(r);
		}
		this.randomNum();
		this.updataView();
	},
	LeftInRow:function(r){
		for(var c=0;c<this.CN-1;c++){
			var nextC=this.nextLeftInRow(r,c);
			if(nextC==-1){
				break;
			}
			else if(this.data[r][c]==0){
				this.data[r][c]=this.data[r][nextC];
				this.data[r][nextC]=0;
				c--;
			}
			else if(this.data[r][c]==this.data[r][nextC]){
				this.data[r][c]=this.data[r][c]*2;
				this.data[r][nextC]=0;
				this.score+=parseInt(this.data[r][c]);
			}
		}
	},
	nextLeftInRow:function(r,c){
		for(var nextC=c+1;nextC<this.CN;nextC++){
			if(this.data[r][nextC]!=0){
				return nextC;
			}
		}
		return -1;
	},
	RightInRow:function(r){
		for(var c=this.CN;c>0;c--){
			var nextC=this.nextRightInRow(r,c);
			if(nextC==-1){
				break;
			}
			else if(this.data[r][c]==0){
				this.data[r][c]=this.data[r][nextC];
				this.data[r][nextC]=0;
				c++;
			}
			else if(this.data[r][c]==this.data[r][nextC]){
				this.data[r][c]=this.data[r][c]*2;
				this.data[r][nextC]=0;
				this.score+=this.data[r][c];
			}
		}
	},
	nextRightInRow:function(r,c){
		for(var nextC=c-1;nextC>=0;nextC--){
			if(this.data[r][nextC]!=0){
				return nextC;
			}
		}
		return -1;
	},
	UpInCol:function(c){
		for(var r=0;r<this.RN-1;r++){
			var nextR=this.nextUpInCol(r,c);
			if(nextR==-1){
				break;
			}
			else if(this.data[r][c]==0){
				this.data[r][c]=this.data[nextR][c];
				this.data[nextR][c]=0;
				r--;
			}
			else if(this.data[r][c]==this.data[nextR][c]){
				this.data[r][c]=this.data[r][c]*2;
				this.data[nextR][c]=0;
				this.score+=this.data[r][c];
			}
		}
	},
	nextUpInCol:function(r,c){
		for(var nextR=r+1;nextR<this.RN;nextR++){
			if(this.data[nextR][c]!=0){
				return nextR;
			}
		}
		return -1;
	},
	DownInCol:function(c){
		for(var r=this.RN-1;r>0;r--){
			var nextR=this.nextDownInCol(r,c);
			if(nextR==-1){
				break;
			}
			else if(this.data[r][c]==0){
				this.data[r][c]=this.data[nextR][c];
				this.data[nextR][c]=0;
				r++;
			}
			else if(this.data[r][c]==this.data[nextR][c]){
				this.data[r][c]=this.data[r][c]*2;
				this.data[nextR][c]=0;
				this.score+=this.data[r][c];
			}
		}
	},
	nextDownInCol:function(r,c){
		for(var nextR=r-1;nextR>-1;nextR--){
			if(this.data[nextR][c]!=0){
				return nextR;
			}
		}
		return -1;
	}
}
window.onload=function(){
	game.start();
}